﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：温度特性
    /// </summary>
    public class R009 : BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;
            int headCount = (int)(Math.Ceiling(head.Dtls.Count / 6d));
            for (int i = 0; i < 1; i++)
            {
                try
                {
                    //1.Customer:
                    if (sheet.GetRow(2).GetCell(headColumnsIndex + 2) != null)
                        sheet.GetRow(2).GetCell(headColumnsIndex + 2).SetCellValue(head.CustomerCode);

                    if (head.Dtls.Count > 0)
                    {
                        sheet.GetRow(2).GetCell(headColumnsIndex + 11).SetCellValue(head.Dtls[0].TestDate.ToString("yyyy/M/d"));
                    }
                    else
                    {
                        sheet.GetRow(2).GetCell(headColumnsIndex + 11).SetCellValue(head.TestDate.ToString("yyyy/M/d"));
                    }

                    sheet.GetRow(3).GetCell(headColumnsIndex + 1).SetCellValue(head.LotNo);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 10).SetCellValue(
                        string.Format("{0} {1}uF/{2}V"
                        , head.Series
                        , head.Cap.ToString()
                        , head.Voltage.ToString()
                        )
                        );
                    sheet.GetRow(3).GetCell(headColumnsIndex + 15).SetCellValue(head.Size);


                    StringBuilder sb = new StringBuilder();
                    string split = "/";
                    sb.Append("Condition: ");
                    if (head.Dtls.Count > 1) sb.Append(head.Dtls[1].Condiction);
                    if (head.Dtls.Count > 2) sb.Append(split + head.Dtls[2].Condiction);
                    if (head.Dtls.Count > 3) sb.Append(split + head.Dtls[3].Condiction);
                    if (head.Dtls.Count > 4) sb.Append(split + head.Dtls[4].Condiction);
                    sheet.GetRow(5).GetCell(headColumnsIndex + 0).SetCellValue(sb.ToString());
                    sb.Length = 0;

                    // less than  176 μA 
                    sheet.GetRow(5).GetCell(headColumnsIndex + 12).SetCellValue(head.LcMax);

                    sheet.GetRow(6).GetCell(headColumnsIndex + 11).SetCellValue(
                            head.Standard.CapMin.ToString()
                            + "-" + head.Standard.CapMax.ToString()
                            );

                    sheet.GetRow(7).GetCell(headColumnsIndex + 12).SetCellValue(head.DfMax / 100d);

                    //Z('-55℃)/Z(20℃)＜3
                    //LC(105℃)＜Eight times specification values 
                    sb.AppendFormat("Z('{0})/Z({1})＜{2}\r\nLC({3})＜Eight times specification values"
                        , head.Dtls.Count > 1 ? head.Dtls[1].Condiction : string.Empty
                        , head.Dtls.Count > 3 ? head.Dtls[3].Condiction : string.Empty
                        , head.ZRate.ToString()
                        , head.Dtls.Count > 4 ? head.Dtls[4].Condiction : string.Empty
                        );
                    sheet.GetRow(9).GetCell(headColumnsIndex + 14).SetCellValue(sb.ToString());
                    sb.Length = 0;

                    //-55℃ /  20℃	
                    if (head.Dtls.Count > 3)
                    {
                        sheet.GetRow(24).GetCell(headColumnsIndex + 13).SetCellValue(
                            head.Dtls[1].Condiction + "/" + head.Dtls[3].Condiction
                            );
                    }
                    //105℃ / LC 20℃
                    if (head.Dtls.Count > 4)
                    {
                        sheet.GetRow(24).GetCell(headColumnsIndex + 15).SetCellValue(
                            head.Dtls[4].Condiction + " / LC " + head.Dtls[3].Condiction
                            );
                    }

                    //Tested by:王燕
                    sheet.GetRow(31).GetCell(headColumnsIndex + 13).SetCellValue("Tested by:" + head.CreateUser);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }



                headColumnsIndex += 29;
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 17;
            int currentDtlColumnIndex = 1;
            int currentHeadRowIndex = 14;
            int dtlColumns = 4;
            int dtlRowCount = 8;

            string RT = "RT:   ";

            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i > 4) break;

                try
                {
                    if (i > 0)
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex).SetCellValue(
                            head.Dtls[i].Condiction
                            );
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }

                #region data
                int cIndex = currentDataColumnIndex;
                var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");
                if (caps.Any())
                {
                    var carpss = caps.ToList();
                    ICellStyle lcCellStyle = workbook.CreateCellStyle();
                    lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                    IDataFormat lcdataformat = workbook.CreateDataFormat();
                    double maxlc = caps.Max(t => t.Value);
                    lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                    //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                    for (int k = 0; k < carpss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (carpss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                    sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                }

                cIndex = currentDataColumnIndex + 2;
                var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                if (dfs.Any())
                {
                    var dfss = dfs.ToList();
                    for (int k = 0; k < dfss.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                            continue;
                        if (dfss[k].Value != 0)
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value / 100, 4));
                    }
                }

                if (i == 0 || i == 3 || i == 4)
                {
                    if (i == 0 || i == 3) cIndex = currentDataColumnIndex + 1;
                    else if (i == 4) cIndex = currentDataColumnIndex + 3;
                    var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = caps.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }
                }

                if (i == 0 || i == 1 || i == 2 || i == 3)
                {
                    cIndex = currentDataColumnIndex + 3;
                    var zs = head.Dtls[i].Datas.Where(d => d.ItemCode == "Z");
                    if (zs.Any())
                    {
                        var zss = zs.ToList();
                        for (int k = 0; k < zss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (zss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(zss[k].Value/1000);
                        }
                    }
                }

                #endregion data

                currentDtlColumnIndex += dtlColumns;
                currentDataColumnIndex += dtlColumns;

                if (i > 0 && (i + 1) % 4 == 0)
                {
                    currentDtlColumnIndex = 1;
                    currentDataColumnIndex = 1;
                    dataRowIndex += dtlRowCount;
                    currentHeadRowIndex += dtlRowCount;
                }

            }

            #endregion

            sheet.ForceFormulaRecalculation = true;
        }
    }
}
